<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力中小企业发展 
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德代驾系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
+----------------------------------------------------------------------
| 添加/编辑乘车人 开发者: 麦沃德科技-半夏
+---------------------------------------------------------------------- -->

<template>
	<view class="container" :style="{'--theme-color': themeColor}" v-if="loadEnd">
		<!-- 表单区 -->
		<view class="container-form">
			<view class="form-item">
				<input class="item-input" v-model="formData.mobile" type="number" maxlength="11" placeholder="请输入手机号码" placeholder-class="item-placeholder" />
			</view>
			<view class="form-item">
				<input class="item-input" v-model="formData.name" type="text" placeholder="姓名(选填)" />
			</view>
		</view>
		<!-- 底部按钮 -->
		<view class="container-footer">
			<view class="footer-btn" @click="handleSubmit()">确认{{passengerId ? "编辑" : "添加"}}</view>
			<view class="safe-padding"></view>
		</view>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	export default {
		data() {
			return {
				// 加载完成
				loadEnd: false,
				// 乘车人id
				passengerId: null,
				// 表单内容
				formData: {
					mobile: "",
					name: "",
				},
				// 延时器
				timeout: null,
			}
		},
		computed: {
			...mapState({
				themeColor: state => state.app.themeColor,
			})
		},
		onLoad(option) {
			if (option && option.id) {
				this.passengerId = option.id
				this.$nextTick(() => {
					uni.setNavigationBarTitle({
						title: "编辑"
					})
				})
				uni.showLoading({
					title: "加载中"
				})
				this.getPassengerDetails(() => {
					uni.hideLoading()
					this.loadEnd = true
				})
			} else {
				this.$nextTick(() => {
					uni.setNavigationBarTitle({
						title: "添加"
					})
				})
				this.loadEnd = true
			}
		},
		onUnload() {
			clearTimeout(this.timeout)
		},
		methods: {
			// 获取乘车人详情
			getPassengerDetails(fn) {
				this.$util.request("passenger.passenger.details", {
					id: this.passengerId
				}).then(res => {
					if (fn) fn()
					if (res.code == 1) {
						this.formData = res.data
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					if (fn) fn()
					console.error('获取乘车人详情 ', error)
				})
			},
			// 添加/编辑
			handleSubmit() {
				if (!this.formData.mobile) {
					uni.showToast({
						icon: "none",
						title: "请输入手机号码",
					})
					return
				}
				if (!this.$util.validation("phone", this.formData.mobile)) {
					uni.showToast({
						icon: "none",
						title: "请输入正确的手机号"
					})
					return
				}
				uni.showLoading({
					title: "加载中",
					mask: true
				})
				var url = ""
				if (this.passengerId) url = "passenger.passenger.edit"
				else url = "passenger.passenger.add"
				this.$util.request(url, this.formData).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						if (this.formData.id) {
							uni.showToast({
								icon: "success",
								title: "编辑成功",
								mask: true,
								duration: 1500
							})
						} else {
							uni.showToast({
								icon: "success",
								title: "添加成功",
								mask: true,
								duration: 1500
							})
						}
						this.timeout = setTimeout(() => {
							uni.navigateBack()
						}, 1500);
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					uni.hideLoading()
					console.error('添加/编辑乘车人 ', error)
				})
			},
		}
	}
</script>

<style lang="scss">
	.container {
		padding: 44rpx 32rpx 144rpx;

		.container-form {
			.form-item {
				margin-top: 32rpx;
				border-radius: 32rpx;
				background: #FFF;
				overflow: hidden;

				&:first-child {
					margin-top: 0;
				}

				.item-input {
					color: #242629;
					font-size: 32rpx;
					font-weight: 600;
					line-height: 44rpx;
					padding: 42rpx 48rpx;
				}

				.item-placeholder {
					color: #242629;
				}
			}
		}

		.container-footer {
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			z-index: 96;
			background: #ffffff;
			border-top: 1rpx solid #F6F7FB;
			padding: 16rpx 24rpx;

			.footer-btn {
				color: #ffffff;
				font-size: 28rpx;
				line-height: 40rpx;
				padding: 20rpx 24rpx;
				border-radius: 40rpx;
				background: var(--theme-color);
				text-align: center;
			}
		}
	}
</style>